package md.lanjing.com.module_deal_center.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.TimeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import com.lxh.baselibray.base.BaseResponce;
import md.lanjing.com.module_deal_center.bean.SalingOrderResponce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.SalingOrderContact;
import retrofit2.Response;

/**
 * 寄售订单-出售中订单
 */
public class SalingFragment extends MvpFragment<SalingOrderContact.SalingOrderPresent> implements SalingOrderContact.ISalingOrderView {
    @BindView(R2.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R2.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.tv_total_num)
    TextView tvTotalNumber;
    @BindView(R2.id.tv_left_number)
    TextView tvLeftCont;
    @BindView(R2.id.tv_per_price)
    TextView tvPerPrice;
    @BindView(R2.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R2.id.iv_weixin)
    ImageView ivWeixin;
    @BindView(R2.id.iv_card)
    ImageView ivCard;
    @BindView(R2.id.iv_ali)
    ImageView ivAli;
    @BindView(R2.id.btn_cancle_order)
    Button btnCancleOrder;
    @BindView(R2.id.linear)
    LinearLayout linearLayout;

    String billId;
    @Override
    public void initData(Bundle savedInstanceState) {
        mPresent.querySalingOrder(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_saling;
    }


    @Override
    public void getSalingOrderData(Response<SalingOrderResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            SalingOrderResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)){
                linearLayout.setVisibility(View.GONE);
                return;
            }
            linearLayout.setVisibility(View.VISIBLE);
            billId=data.getBillId();
            tvOrderNumber.setText("订单号：" + data.getBillId());
            tvOrderTime.setText("订单时间：" + TimeUtils.millis2String(data.getCreateTime()));
            tvCoinType.setText("币种：" + data.getType());
            String tokenNum = data.getTokenNum();
            String stockTokenNum = data.getStockTokenNum();
            String cnyPrice = data.getCnyPrice();
            tvTotalNumber.setText("总量：" +tokenNum);
            tvLeftCont.setText("剩余：" + stockTokenNum);
            tvPerPrice.setText("单价：" + cnyPrice + "CNY");
            tvTotalPrice.setText("总价：" + Double.valueOf(tokenNum) * Double.valueOf(cnyPrice) + "CNY");
            if (data.getAliPay() == 1) {
                ivAli.setVisibility(View.VISIBLE);
            } else {
                ivAli.setVisibility(View.GONE);
            }
            if (data.getWechat() == 1) {
                ivWeixin.setVisibility(View.VISIBLE);
            } else {
                ivWeixin.setVisibility(View.GONE);
            }
            if (data.getBank() == 1) {
                ivCard.setVisibility(View.VISIBLE);
            } else {
                ivCard.setVisibility(View.GONE);
            }
        } else {
            linearLayout.setVisibility(View.GONE);
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getCancleOrderData(Response<BaseResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "取消订单成功");
            linearLayout.setVisibility(View.GONE);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected SalingOrderContact.SalingOrderPresent createPresent() {
        return new SalingOrderContact.SalingOrderPresent();
    }


    @OnClick(R2.id.btn_cancle_order)
    public void onViewClicked() {
        showDealDialog();
    }

    AlertDialog dealDialog = null;
    private void showDealDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    mPresent.cancleSalingOrder(context,billId,dealPwd);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }
}
